<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>{$appConfigs.title}</title>

    <link rel="stylesheet" type="text/css" href="{$contextPath}/pas/easyui/themes/{$page_theme | default: 'default'}/easyui.css">
    <link rel="stylesheet" type="text/css" href="{$contextPath}/pas/easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="{$contextPath}/pas/style/my.css">
    <script type="text/javascript" src="{$contextPath}/pas/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="{$contextPath}/pas/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="{$contextPath}/pas/javascript/my.js"></script>
    
</head> 
<body style="width:99%;padding:0px;">
	<div class="easyui-layout" style="width:100%;height:50%;padding:0px;">
		<div data-options="region:'center',split:true">
			<div id="c" class="easyui-panel" title="投资策略" style="width:100%;padding:0px;"> 
				{datagrid id="dg" template="/common/datagrid2.tpl" datasource="/invest/StrategyService/findAll.do?invest_id=`$Request.invest_id`" view="i_strategy"}
			</div>
			<div id="strategy_detail" class="easyui-panel" title="策略计划" style="width:99%;height:78%">
			</div>
		</div>
		<div data-options="region:'east',split:false" style="width:25%;">
			<div id="s" class="easyui-panel" title="策略配置参数" style="width:100%;">
				<table id="pg" class="easyui-propertygrid" style="width:100%" data-options="
							url2:'/invest/props/find.do?v=i_strategy_value_average&m=invest&s=StrategyValueAverageService:findById&id=14',
							method:'get',
							showHeader:true,
							showGroup:false,
							scrollbarSize:0
						">
				</table>
			</div>
		</div>
	</div>

    {form id="dlg" view="i_strategy" }
    {form id="StrategyValueAverageService" view="i_strategy_value_average" }
    {form id="StrategyGridService" view="i_strategy_grid" }
    {form id="StrategyTrendService" view="i_strategy_trend" }
    {form id="StrategyPriceChangeService" view="i_strategy_price_change" }
    {form id="StrategyAutoPlanService" view="i_strategy_auto_plan" }
    {form id="StrategyHouseService" view="i_strategy_house" }
    
    <script>
    var invest_id={$Request.invest_id};
    {literal}
    field_formatters.effective_from=function (value, row, index){
		if(value)
			return value.substring(0,10);
    }
    field_formatters.button=function (value, row, index){
		return "<a class='easyui-linkbutton' onclick='selectStrategy("+row['strategy_id']+","+index+")'>选中</a>";
    }
    field_formatters.effective_to=function (value, row, index){
		if(value)
			return value.substring(0,10);
    }
    field_formatters.scope=function (value, row, index){
		if(value == 0)
			return '共有策略';
		else if(value == 1)
			return '私有策略';
    }
    field_formatters.status=function (value, row, index){
		if(value == 0)
			return '<font color="red">暂停</font>';
		else if(value == 1)
			return '<font color="green">执行中</font>';
		else if(value == 2)
			return '<font color="red">已结束</font>';
		else if(value == 3)
			return '<font color="green">执行中（仅卖出）</font>';
    }
    field_formatters.transaction_type=function (value, row, index){
		if(value == -1)
			return '<font color="red">卖出</font>';
		else if(value == 1)
			return '<font color="green">买入</font>';
    }
    action_prehandlers.createOne=function(){
    	return {"invest_id":invest_id};
    }
    
	function selectStrategy(strategy_id, rowIndex, rowData) {
	    var row = rowData;
	    if(!row) {
	    	row = getDatagridRow('dg', rowIndex);
	    }
	    if(!strategy_id) {
	    	strategy_id = row['strategy_id'];
	    }
	    if(row) {
	    	var service = row.strategyType.service;
			if(service == "StrategyValueAverageService") {
				getDatagrid("pg").datagrid('reload', '/invest/props/find.do?v=i_strategy_value_average&m=invest&s=StrategyValueAverageService:findById&id='+strategy_id);
				$("#strategy_detail").html('<iframe name="detail" scrolling="auto" src="/invest/invest/strategy_value_average_detail.shtml?strategy_id='+strategy_id+'" frameborder="0" style="width:99%;height:99%;"></iframe>');
			} else if(service == "StrategyGridService") {
				getDatagrid("pg").datagrid('reload', '/invest/props/find.do?v=i_strategy_grid&m=invest&s=StrategyGridService:findById&id='+strategy_id);
				$("#strategy_detail").html('<iframe name="detail" scrolling="auto" src="/invest/invest/strategy_grid_detail.shtml?strategy_id='+strategy_id+'" frameborder="0" style="width:99%;height:99%;"></iframe>');
			} else if(service == "StrategyTrendService") {
				getDatagrid("pg").datagrid('reload', '/invest/props/find.do?v=i_strategy_trend&m=invest&s=StrategyTrendService:findById&id='+strategy_id);
				$("#strategy_detail").html('<iframe name="detail" scrolling="auto" src="/invest/invest/strategy_trend_detail.shtml?strategy_id='+strategy_id+'" frameborder="0" style="width:99%;height:99%;"></iframe>');
			} else if(service == "StrategyPriceChangeService") {
				getDatagrid("pg").datagrid('reload', '/invest/props/find.do?v=i_strategy_price_change&m=invest&s=StrategyPriceChangeService:findById&id='+strategy_id);
				$("#strategy_detail").html('<iframe name="detail" scrolling="auto" src="/invest/invest/strategy_price_change.shtml?strategy_id='+strategy_id+'" frameborder="0" style="width:99%;height:99%;"></iframe>');
			} else if(service == "StrategyAutoPlanService") {
				getDatagrid("pg").datagrid('reload', '/invest/props/find.do?v=i_strategy_auto_plan&m=invest&s=StrategyAutoPlanService:findById&id='+strategy_id);
				$("#strategy_detail").html('<iframe name="detail" scrolling="auto" src="/invest/invest/strategy_auto_plan_detail.shtml?strategy_id='+strategy_id+'" frameborder="0" style="width:99%;height:99%;"></iframe>');
			} else if(service == "StrategyHouseService") {
				getDatagrid("pg").datagrid('reload', '/invest/props/find.do?v=i_strategy_house&m=invest&s=StrategyHouseService:findById&id='+strategy_id);
				$("#strategy_detail").html('<iframe name="detail" scrolling="auto" src="/invest/invest/strategy_house_detail.shtml?strategy_id='+strategy_id+'" frameborder="0" style="width:99%;height:99%;"></iframe>');
			}
			
	    } else {
			alert('没有选择投资策略！');
	    }
	}

    function configStrategy() {
	    var row = getDatagrid('dg').datagrid('getSelected');
	    if(row) {
			var configs = {};
	    	for (let key of Object.keys(row.strategyConfig)) {
	    		configs[key]=row.strategyConfig[key];
	    	}
			//
	    	var service = row.strategyType.service;
			if(service == "StrategyValueAverageService") {
				if(row.strategyConfig.fell_rate) {
					configs.fell_rate = configs.fell_rate * 100;
				}
				if(row.strategyConfig.rise_rate) {
					configs.rise_rate = configs.rise_rate * 100;
				}
			}
	    	updateOne(service,"/invest/"+service+"/config.do","dg",configs);
	    } else {
			alert('没有选择投资策略！');
	    }
    }
    
    function removeStrategy() {
	    removeOne("/invest/StrategyService/remove.do","strategy_id","dg",{},reloadStrategy);
    }

	function reloadStrategy() {
		reload("pg");
		reload("dg2");
	}

	function saveOne(dlg){
		var url = getForm(dlg).attr('action');
		var dg = getForm(dlg).attr('dg');
	    var row = getDatagrid('dg').datagrid('getSelected');
		
		$('#js-waiting').show();
		getForm(dlg).form('submit',{
			url: url,
			onSubmit: function(){
				return $(this).form('validate');
			},
			success: function(result){
				var result = eval('('+result+')');
				if (result.ReturnCode == 0){
					$.messager.show({
						title: '提示',
						msg: '操作成功！'
					});
					getDialog(dlg).dialog('close');
					$('#js-waiting').hide();
					reload(dg);
					selectStrategy(null,null,row);
				} else {
					$('#js-waiting').hide();
					$.messager.show({
						title: '错误',
						msg: result.Msg
					});
				}
			}
		});
	}

    {/literal}
    </script>
</body>
</html>